<?php

class User extends CI_Controller {



   function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->library('session');	
		$this->load->database();
		$this->load->library('form_validation');
		$this->load->model('welcome_mdl');
		$this->check_login();
    } 

    public function check_login()
	{
		if($this->session->userdata('user_data_admin') == false)
		{
			$this->load->view('admin/signin');
		}
	}
	function index()
	{
		if($this->session->userdata('user_data_admin') == false)
		{
			$this->load->view('admin/signin');
		}
		else
		{
			$data__header['page_name'] = 'dashboard';
        	$this->load->view('admin/header',$data__header);	
        	$this->load->view('admin/dashboard');		
		}			

	}



    /**

    * encript the password 

    * @return mixed

    */	

    function __encrip_password($password) {

       // return md5($password);

        return ($password);

    }	



    /**

    * check the username and the password with the database

    * @return void

    */

	function validate_credentials()

	{	



		$this->load->model('admin/Users_model');



		$user_name = $this->input->post('user_name');

		$password = $this->__encrip_password($this->input->post('password'));



		$is_valid = $this->Users_model->validate($user_name, $password);

		

		if($is_valid->num_rows == 1)

		{

			$data = array(

				'adminuser_name' => $user_name,

				'adminuser_type' => $is_valid->row("user_type"),

				'is_logged_in' => true

			);

			$this->session->set_userdata($data);

			redirect('admin/home');

		}

		else // incorrect username or password

		{

			$data['message_error'] = TRUE;

			$this->load->view('admin/login', $data);	

		}

	}	



	

	/**

    * Destroy the session, and logout the user.

    * @return void

    */		

	function logout()

	{

		$this->session->sess_destroy();

		redirect('admin');

	}



}